#get package to read in data from stata and other packages
library(foreign)
library(reshape2)
library(dplyr)
library(ggplot2)
library(grid)
library(gridExtra)
require(gridExtra)
library(cowplot)
library(gdata)



get_legend<-function(myggplot){
  tmp <- ggplot_gtable(ggplot_build(myggplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}


#read in data
dockettermData <- read.xls("predValuesForStackedBar.xls")
dockettermData <- dockettermData[5:69,]
attach(dockettermData)


#make time series
docketterm44 <- ts(dockettermData, start=c(1951), end=c(2015))
docketterm44
docketterm44 [1:10]
is.ts(docketterm44)



##################
##################
####### MED ######
##################
##################


#######################################
#UNCONSTRAINED counterfactual bar plot#
#######################################

#make dataframe so can use better plotting packages
docketterm44medun.df <- data.frame(term=term, 
                                   phatmednb=phatmednb,
                                   medDist_predDiff=medDist_predDiff)

#transform term
docketterm44medun.df$term.f <- factor(docketterm44medun.df$term)
is.factor(docketterm44medun.df$term.f)


#melt the data to use with ggplot2
mdocketterm44medun.df <- melt(docketterm44medun.df, id=c("term", "term.f"))


#make factor var for different lines
mdocketterm44medun.df$predType <- 999
mdocketterm44medun.df$predType[mdocketterm44medun.df$variable == "phatmednb"] <- 2
mdocketterm44medun.df$predType[mdocketterm44medun.df$variable == "medDist_predDiff"] <- 1
is.factor(mdocketterm44medun.df$predType)
is.numeric(mdocketterm44medun.df$predType)
mdocketterm44medun.df$predType.f <- factor(mdocketterm44medun.df$predType, labels = c("Unconstrained Predicted Values", "Predicted Values"))
mdocketterm44medun.df$predType.f
is.factor(mdocketterm44medun.df$predType.f)


#stacked bar plot UNCONSTRAINED counterfactual
medStackedBarUncon <- ggplot(mdocketterm44medun.df[order(mdocketterm44medun.df$predType,decreasing=T),],aes(x=term, y=value, fill=predType.f)) + 
  geom_bar(stat="identity") +
  ylab(label="Number of Granted Cases") + 
  xlab("Term") +
  ggtitle("Unconstrained") +
  coord_cartesian(ylim=c(70,200)) +
  scale_y_continuous(breaks=seq(70, 200, 10)) +
  scale_x_continuous(breaks=seq(1950, 2015, 5)) +
  scale_fill_manual(values = c("black","gray")) +
  theme_bw() +
  theme(axis.text=element_text(colour="black",size=18),
        axis.title=element_text(size=18),
        axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=18)) +
  theme(panel.grid.major = element_line(colour = "gray67")) +
  theme(panel.grid.minor.x = element_blank()) +
  theme(legend.text=element_text(size=18))+ 
  theme(legend.title=element_blank()) +
  theme(legend.position="bottom") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(plot.title = element_text(face="bold")) +
  #geom_text(x=2005, y=160, label="172 Cases \n Avoided", size = 7, fill = "white") +
  annotate("text", x=2005, y=155, 
           label = "182 Cases \n Avoided", fontface = 'bold', size = 7) +
  guides(fill = guide_legend(reverse=TRUE))
medStackedBarUncon




###########################################
#CONSTRAINED counterfactual bar plot MAX #
###########################################


#make dataframe so can use better plotting packages
docketterm44medcon.df <- data.frame(term=term, 
                                    #phatmednb=phatmednb,
                                    phatmedDist99=phatmedDist99,
                                    pos_medDist_predDiff99=pos_medDist_predDiff99)


#transform term
docketterm44medcon.df$term.f <- factor(docketterm44medcon.df$term)
is.factor(docketterm44medcon.df$term.f)


#melt the data to use with ggplot2
mdocketterm44medcon.df <- melt(docketterm44medcon.df, id=c("term", "term.f"))


#make factor var for different lines
mdocketterm44medcon.df$predType <- 999
mdocketterm44medcon.df$predType[mdocketterm44medcon.df$variable == "phatmedDist99"] <- 2
mdocketterm44medcon.df$predType[mdocketterm44medcon.df$variable == "pos_medDist_predDiff99"] <- 1
is.factor(mdocketterm44medcon.df$predType)
is.numeric(mdocketterm44medcon.df$predType)
mdocketterm44medcon.df$predType.f <- factor(mdocketterm44medcon.df$predType, labels = c("Predicted Values  ","Constrained Predicted Values"))
mdocketterm44medcon.df$predType.f
is.factor(mdocketterm44medcon.df$predType.f)


#stacked bar plot UNCONSTRAINED counterfactual
medStackedBarCon <- ggplot(mdocketterm44medcon.df[order(mdocketterm44medcon.df$predType,decreasing=F),],aes(x=term, y=value, fill=predType.f)) + 
  geom_bar(stat="identity") +
  ylab(label="Number of Granted Cases") + 
  ggtitle("Maximum Constraint") +
  xlab("Term") +
  coord_cartesian(ylim=c(70,200)) +
  scale_y_continuous(breaks=seq(70, 200, 10)) +
  scale_x_continuous(breaks=seq(1950, 2015, 5)) +
  scale_fill_manual(values = c("black","gray90")) +
  theme_bw() +
  theme(axis.text=element_text(colour="black",size=18),
        axis.title=element_text(size=18),
        axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=18)) +
  theme(panel.grid.major = element_line(colour = "gray67")) +
  theme(panel.grid.minor.x = element_blank()) +
  theme(legend.text=element_text(size=18))+ 
  theme(legend.title=element_blank()) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(plot.title = element_text(face="bold")) +
  annotate("text", x=2005, y=150, 
           label = "779 Cases \n Granted \n Review", fontface = 'bold', size = 7) +
  theme(legend.position="bottom")
medStackedBarCon



###########################################
#CONSTRAINED counterfactual bar plot MEAN #
###########################################


#make dataframe so can use better plotting packages
docketterm44medconmean.df <- data.frame(term=term, 
                                        #phatmednb=phatmednb,
                                        phatmedDistmean=phatmedDistmean,
                                        pos_medDist_predDiffmean=pos_medDist_predDiffmean)


#transform term
docketterm44medconmean.df$term.f <- factor(docketterm44medconmean.df$term)
is.factor(docketterm44medconmean.df$term.f)


#melt the data to use with ggplot2
mdocketterm44medconmean.df <- melt(docketterm44medconmean.df, id=c("term", "term.f"))


#make factor var for different lines
mdocketterm44medconmean.df$predType <- 999
mdocketterm44medconmean.df$predType[mdocketterm44medconmean.df$variable == "phatmedDistmean"] <- 2
mdocketterm44medconmean.df$predType[mdocketterm44medconmean.df$variable == "pos_medDist_predDiffmean"] <- 1
is.factor(mdocketterm44medconmean.df$predType)
is.numeric(mdocketterm44medconmean.df$predType)
mdocketterm44medconmean.df$predType.f <- factor(mdocketterm44medconmean.df$predType, labels = c("Predicted Values  ","Constrained Predicted Values"))
mdocketterm44medconmean.df$predType.f
is.factor(mdocketterm44medconmean.df$predType.f)


#stacked bar plot CONSTRAINED counterfactual
medStackedBarConMean <- ggplot(mdocketterm44medconmean.df[order(mdocketterm44medconmean.df$predType,decreasing=F),],aes(x=term, y=value, fill=predType.f)) + 
  geom_bar(stat="identity") +
  ylab(label="Number of Granted Cases") + 
  xlab("Term") +
  ggtitle("Mean Constraint") +
  coord_cartesian(ylim=c(70,200)) +
  scale_y_continuous(breaks=seq(70, 200, 10)) +
  scale_x_continuous(breaks=seq(1950, 2015, 5)) +
  scale_fill_manual(values = c("black","gray90")) +
  theme_bw() +
  theme(axis.text=element_text(colour="black",size=18),
        axis.title=element_text(size=18),
        axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=18)) +
  theme(panel.grid.major = element_line(colour = "gray67")) +
  theme(panel.grid.minor.x = element_blank()) +
  theme(legend.text=element_text(size=18))+ 
  theme(legend.title=element_blank()) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(plot.title = element_text(face="bold")) +
  annotate("text", x=2005, y=150, 
           label = "149 Cases \n Granted \n Review", fontface = 'bold', size = 7) +
  theme(legend.position="bottom")
medStackedBarConMean



#stack the plots
#must require gridExtra to make this work
stackedBarCombo <- grid.arrange(medStackedBarUncon, medStackedBarConMean, medStackedBarCon, ncol=3, nrow =1)














